package com.kuaiying.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import zyl.nlp.tools.file.divideWords.NlpirMethod;

public class StringUtils {
	
	private static String reg_charset = "([a-z]*)([A-Z]*)([0-9]*)([\u4E00-\u9FA5]*)"; 
	private static Pattern p = Pattern.compile(reg_charset); 
	
	public static String filterZh( String line ){
		Matcher m = p.matcher(line);
		String text = "";
		while (m.find()) 
	    {
			text += " " + m.group();
	    }
		return text;
	}
    
	/**
	 * 去除字符串中的[]內容
	 * 
	 * @param line
	 * @return
	 */
	public static String moveUrl(String line) {
		return line.replaceAll("\\[(.*)\\]", "");
	}

	/**
	 * 对字符串分词
	 * 
	 * @param line
	 * @return
	 */
	public static String segLine(String line) {
		if (NlpirMethodinit() == false) {
			return null;
		}
		return NlpirMethod.NLPIR_ParagraphProcess(line, 1);
	}

	/**
	 * 获取字符串关键词
	 * 
	 * @param line
	 * @return
	 */
	public static String getKeyword(String line) {
		if (NlpirMethodinit() == false || line.length() < 50) {
			return null;
		}
		try{
			return NlpirMethod.NLPIR_GetKeyWords(line);
		}catch (Exception e) {
			return "";
		}
	}

	private static boolean NlpirMethodinit() {
		if (NlpirMethod.Nlpir_init() == false) {
			boolean tag = NlpirMethod.Nlpir_init();
			return tag;
		}
		return true;
	}
	/**
	 * 获取随机中文
	 * 
	 * @param nums
	 *            个数
	 * @return
	 */
	public static String getRandomStr(int nums, String keys) {
		byte[] by = new byte[2];
		int max = 176;
		int min = 247;
		Random random = new Random(System.nanoTime());
		int pos = random.nextInt(max) % (max - min + 1) + min;

		// 插入5个随机字符
		StringBuffer rInStr = new StringBuffer("");
		for (int i = 0; i < nums; i++) {
			max = 247;
			min = 176;
			random = new Random(System.nanoTime());
			pos = random.nextInt(max) % (max - min + 1) + min;
			by[0] = (byte) pos;
			max = 254;
			min = 161;
			random = new Random(System.nanoTime());
			pos = random.nextInt(max) % (max - min + 1) + min;
			by[1] = (byte) pos;
			try {
				rInStr.append(new String(by, "GB2312"));
			} catch (UnsupportedEncodingException e) {
				rInStr.append( keys );
			}
			if( i % 1000 == 500 ){
				rInStr.append( keys );
			}
		}
		return rInStr.toString();
	}
	
	public static String toUper( String input){
		return input.toUpperCase();
	}
	public static void main(String[] args) throws IOException {
		/*
		String nline = StringUtils.moveUrl("2013新教科版物理九年第三章《认识电路》word同步测试[http://www.qqgb.com/Program/Java/JavaFAQ/JavaJ2SE/Program_146959.html].doc");
		System.out.println( nline );
		*/
		System.out.println( StringUtils.getRandomStr(10, "asda"));
	}
}
